package org.example.controller;

import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.repository.Deployment;
import org.example.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class TestController {

    @Autowired
    TestService testService;

    @GetMapping("/test1")
    public String test1() {
        return testService.test1();
    }

    @GetMapping("/test2")
    public String test2() {
        return testService.test2();
    }

    /** 部署流程定义 */
    @GetMapping("/deployWithClassPath")
    public String deployWithClassPath() {
        RepositoryService rs = ProcessEngines.getDefaultProcessEngine().getRepositoryService();
        Deployment deployment = rs.createDeployment()
                .name("测试流程")
                .addClasspathResource("processes/test.bpmn")
                .deploy();
        rs.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
        return deployment.getId();
    }

    /** 开启一个流程实例 */
    @GetMapping("/startProcess")
    public String startProcess() {
        return testService.startProcess();
    }

    /** 查询当前实例对应的任务进度 */
    @GetMapping("/queryCurrentTask")
    public String queryCurrentTask(@RequestParam(value = "processInstanceId") String processInstanceId) {
        return testService.queryCurrentTask(processInstanceId);
    }

    /** 销售经理审核 */
    @GetMapping("/salesReview")
    public String salesReview(@RequestParam(value = "taskId") String taskId) {
        return testService.salesReview(taskId);
    }

    /** 财务经理审核 */
    @GetMapping("/financeReview")
    public String financeReview(@RequestParam(value = "taskId") String taskId) {
        return testService.financeReview(taskId);
    }

    /** 查询实例的历史流程 */
    @GetMapping("/queryHistoryActivity")
    public List<HistoricActivityInstance> queryHistoryActivity(@RequestParam(value = "processInstanceId") String processInstanceId) {
        return testService.queryHistoryActivity(processInstanceId);
    }

    /** 查询实例的历史任务 */
    @GetMapping("/queryHistoryTask")
    public List<HistoricTaskInstance> queryHistoryTask(@RequestParam(value = "processInstanceId") String processInstanceId) {
        return testService.queryHistoryTask(processInstanceId);
    }
}
